package ru.ssau;

import java.util.logging.Logger;
import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;

/**
 *
 * @author Smike
 */
public class ContextUtils {

    private static final Logger LOGGER = Logger.getLogger(ContextUtils.class.getName());
    
    public static interface Callback {
        void onNewString(String string);
    }
    
    public static final void listContext(Context ctx, String indent, String addon, Callback callback) {
        try {
            NamingEnumeration list = ctx.listBindings("");
            while (list.hasMore()) {
                Binding item = (Binding) list.next();
                String className = item.getClassName();
                String name = item.getName();
                callback.onNewString(addon + indent + className + " " + name);
                Object o = item.getObject();
                if (o instanceof javax.naming.Context) {
                    listContext((Context) o, indent + " ", addon + "_", callback);
                }
            }
        } catch (NamingException ex) {
            LOGGER.info("oops");
        }

    }
}
